package com.zr.shortlink.admin.controller;

import com.zr.shortlink.admin.common.result.Result;
import com.zr.shortlink.admin.common.result.Results;
import com.zr.shortlink.admin.dto.req.UserLoginReqDTO;
import com.zr.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.zr.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.zr.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.zr.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;



    @GetMapping("api/shortlink/admin/v1/user/{username}")
    public  String getUserByUsername(@PathVariable("username") String username){
        return userService.getUserByUsername(username).toString();
    }

    /**
     * 用户注册
     * @param requestparam
     * @return
     */
    @PostMapping("api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestparam){
        userService.register(requestparam);
        return Results.success();
    }

    /**
     * 修改用户信息
     * @param requestparam
     * @return
     */
    @PutMapping("api/short-link/v1/admin/user/{username}")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestparam){
        userService.update(requestparam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestparam
     * @return
     */
    @PostMapping("api/short-link/admin/v1/user/login")
    public  Result<UserLoginRespDTO> hasUsername(@RequestBody UserLoginReqDTO requestparam){
        log.info("用户登录{}",requestparam);
        UserLoginRespDTO result = userService.login(requestparam);
        return Results.success(result);
    }

    /**
     * 检查用户是否登录
     * @param token
     * @return
     */
    @GetMapping("api/short-link/admin/v1/user/checkLogin")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token ){

        return Results.success(userService.checkLogin(username,token));
    }
}
